package demo4;

import java.util.Stack;

public class Solutionstack {
    //括号匹配
    public boolean isVaild(String s){
        Stack<Character> stack1=new Stack<>();
        for(int i=0;i<s.length();i++){
            char ch=s.charAt(i);
            if(ch=='('||ch=='{'||ch==']'){
                stack1.push(ch);
            }else{
                if(stack1.isEmpty()){
                    return false;
                }
                char ch2=stack1.peek();
                if((ch==')'&&ch2=='(')||(ch==']'&&ch2=='[')||(ch=='}'&&ch2=='{')){
                    stack1.pop();
                }else{
                    return false;
                }
            }
        }
        if(stack1.isEmpty()){
            return true;
        }else{
            return false;
        }
    }
}
